<?php

class Core_Table_Permission_Accredit_Action extends Kd_Data_Table {

    public function __construct() {
        parent::__construct();
    }

    public function insertRoleRelation($accreditId, array $roldIds, $transaction = NULL) {
        $insert = Kd_Data_Factory::getInsertDcl()
                        ->into('{t}permission_role_accredit')
                        ->value('accredit_type', ':type:s')
                        ->value('accredit_id', ':accreditId:i')
                        ->value('role_id', ':roleId:i')
                        ->toSql();
        $driver = $this->getDbDriver($transaction);
        foreach ($roldIds as $roleId) {
            $driver->setSql($insert, array('type' => 'component', 'accreditId' => $accreditId, 'roleId' => $roleId))->execute();
        }
        return $this;
    }

    public function deleteRoleRelation($accreditId, $transaction = NULL) {
        Kd_Data_Factory::getDeleteDcl()
                ->from('{t}permission_role_accredit')
                ->where()
                ->eq('accredit_id', ':accreditId:i')
                ->eq('accredit_type', ':type:s')
                ->closest()
                ->setDbDriver($this->getDbDriver($transaction))
                ->setParameters(array('type' => 'component', 'accreditId' => $accreditId))
                ->execute();
        return $this;
    }

    public function getRoleIds($accreditId) {
        return Kd_Data_Factory::getQueryDcl()
                ->from('{t}permission_role_accredit')
                ->where()->eq('accredit_id', ':accreditId:i')->endSelf()
                ->addParameter('accreditId', $accreditId)
                ->setDbDriver($this->getDbDriver())
                ->fetch()
                ->getFieldValues('role_id');
    }

}